using System;
using Microsoft.Xna.Framework;

namespace MomentumV2
{

    /// <summary>
    /// Enum describes the screen transition state.
    /// </summary>
    public enum ScreenState
    {
        TransitionOn,
        Active,
        TransitionOff,
        Hidden,
    }

    /// <summary>
    /// A screen is a single layer that has update and draw logic, and which
    /// can be combined with other layers to build up a complex menu system.
    /// For instance the main menu, the options menu, the "are you sure you
    /// want to quit" message box, and the main game itself are all implemented
    /// as screens.
    /// </summary>
    public abstract class mGameScreen
    {

        #region Instance Variables

        bool m_IsPopup;
        TimeSpan m_TransitionOnTime = TimeSpan.Zero;
        TimeSpan m_TransitionOffTime = TimeSpan.Zero;
        float m_TransitionPosition = 1;
        ScreenState m_ScreenState = ScreenState.TransitionOn;
        bool m_IsExiting;
        bool m_OtherScreenHasFocus;
        mScreenManager m_ScreenManager;

        #endregion

        #region Properties

        public bool IsPopup
        {
            get { return m_IsPopup; }
            protected set { m_IsPopup = value; }
        }

        public TimeSpan TransitionOnTime
        {
            get { return m_TransitionOnTime; }
            protected set { m_TransitionOnTime = value; }
        }

        public TimeSpan TransitionOffTime
        {
            get { return m_TransitionOffTime; }
            protected set { m_TransitionOffTime = value; }
        }

        public float TransitionPosition
        {
            get { return m_TransitionPosition; }
        }

        public byte TransitionAlpha
        {
            get { return (byte)(255 - m_TransitionPosition * 255); }
        }

        public ScreenState ScreenState
        {
            get { return m_ScreenState; }
        }

        public bool IsExiting
        {
            get { return m_IsExiting; }
            protected internal set { m_IsExiting = value; }
        }

        public bool IsActive
        {
            get
            {
                return !m_OtherScreenHasFocus &&
                       (m_ScreenState == ScreenState.TransitionOn ||
                        m_ScreenState == ScreenState.Active);
            }
        }

        public mScreenManager ScreenManager
        {
            get { return m_ScreenManager; }
            internal set { m_ScreenManager = value; }
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Load graphics content for the screen.
        /// </summary>
        public virtual void LoadContent() { }

        /// <summary>
        /// Unload content for the screen.
        /// </summary>
        public virtual void UnloadContent() { }

        #endregion

        #region Update

        /// <summary>
        /// Allows the screen to run logic, such as updating the transition position.
        /// Unlike HandleInput, this method is called regardless of whether the screen
        /// is active, hidden, or in the middle of a transition.
        /// </summary>
        public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                      bool coveredByOtherScreen)
        {
            m_OtherScreenHasFocus = otherScreenHasFocus;

            if (m_IsExiting)
            {
                m_ScreenState = ScreenState.TransitionOff;
                if (!UpdateTransition(gameTime, m_TransitionOffTime, 1))
                {
                    m_ScreenManager.RemoveScreen(this);
                }
            }
            else if (coveredByOtherScreen)
            {
                if (UpdateTransition(gameTime, m_TransitionOffTime, 1))
                {
                    m_ScreenState = ScreenState.TransitionOff;
                }
                else
                {
                    m_ScreenState = ScreenState.Hidden;
                }
            }
            else
            {
                if (UpdateTransition(gameTime, m_TransitionOnTime, -1))
                {
                    m_ScreenState = ScreenState.TransitionOn;
                }
                else
                {
                    m_ScreenState = ScreenState.Active;
                }
            }
        }

        /// <summary>
        /// Helper for updating the screen transition position.
        /// </summary>
        bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
        {
            float transitionDelta;

            if (time == TimeSpan.Zero)
            {
                transitionDelta = 1;
            }
            else
            {
                transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
                                          time.TotalMilliseconds);
            }
            m_TransitionPosition += transitionDelta * direction;
            if ((m_TransitionPosition <= 0) || (m_TransitionPosition >= 1))
            {
                m_TransitionPosition = MathHelper.Clamp(m_TransitionPosition, 0, 1);
                return false;
            }
            return true;
        }

        /// <summary>
        /// Allows the screen to handle user input. Unlike Update, this method
        /// is only called when the screen is active, and not when some other
        /// screen has taken the focus.
        /// </summary>
        public virtual void HandleInput(mInputState input) { }

        #endregion

        #region Draw

        /// <summary>
        /// This is called when the screen should draw itself.
        /// </summary>
        public virtual void Draw(GameTime gameTime) { }

        #endregion

        #region Methods

        /// <summary>
        /// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
        /// instantly kills the screen, this method respects the transition timings
        /// and will give the screen a chance to gradually transition off.
        /// </summary>
        public void ExitScreen()
        {
            if (TransitionOffTime == TimeSpan.Zero)
            {
                // If the screen has a zero transition time, remove it immediately.
                ScreenManager.RemoveScreen(this);
            }
            else
            {
                // Otherwise flag that it should transition off and then exit.
                m_IsExiting = true;
            }
        }

        #endregion

    }
}
